<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class Roles extends Model
{
    protected $table = 'roles';
    public $timestamps = true;


    /**
     * $where['page'] :offset
     * $where['length'] :每页长度
     * $where['keywords'] :搜索词
     * @param $where
     * @return array
     */
    public static function getList($where)
    {
        $page = $where['page'] - 1;
        $length = $where['length'];
        $keywords = "%" . $where['keywords'] . "%";

        $sql = "select roles.id,roles.name as role_name, group_concat( '',users.username) as user_names from roles
              left join model_has_roles mhr on roles.id = mhr.role_id left join users
              on mhr.model_id=users.id where roles.name like ? group by roles.id limit ?,?";

        $rt = DB::select($sql, [$keywords, $page, $length]);
        return $rt;
    }

    /**
     * 获取组内成员
     * @param $where
     * @return array
     */
    public static function getGroupUsers($where)
    {
        $role_id = $where['role_id'];
        $sql = "select u.id as userid,u.username,u.email,mhr.role_id  from model_has_roles mhr left join users u on mhr.model_id=u.id where mhr.role_id=? ;";
        $rt = DB::select($sql, [ $role_id]);
        return $rt;
    }

    /**
     * 条件查询菜单
     * @param $where
     * @param $flag 1:字段有id;0：字段没有id
     * @return array
     */
    public static function getListByWhere($where, $flag = 1)
    {
        if ($flag) {
            $rt = DB::table('permissions')->where('id', '<>', $where['id'])
                ->where(function ($query) use ($where) {
                    $query->where('title', '=', $where['title'])
                        ->orWhere('name', '=', $where['name'])
                        ->orWhere('url', '=', $where['url']);
                })->get()->toArray();
        } else {
            $rt = DB::table('permissions')
                ->where(function ($query) use ($where) {
                    $query->where('title', '=', $where['title'])
                        ->orWhere('name', '=', $where['name'])
                        ->orWhere('url', '=', $where['url']);
                })->get()->toArray();
        }
        return $rt;
    }

    /**
     * 删除菜单
     * @param $where
     * @return false|int
     */
    public function delByWhereIn($where = [])
    {
        if ($where) {
            return DB::table('permissions')->whereIn('id', $where)->delete();
        } else {
            return false;
        }
    }

    /**
     * 保存组用户
     * @param $where
     * @return bool
     */
    public static function saveGroupUsers($where)
    {
        try{
            DB::transaction(function () use ($where) {

                $adds=$where['add'];
                $role_id=$where['role_id'];
                $model_ids=$where['model_ids'];

                if($adds){
                    DB::table('model_has_roles')->insert($adds);
                }

                if($model_ids){
                    DB::table('model_has_roles')->where('role_id','=' ,$role_id)->whereIn('model_id',$model_ids)->delete();
                }

            });
            return true;
        }catch (\Exception $exception){

            return false;
        }
    }

    /**
     * 删除组
     * 附带的model_has_roles
     * 附带的role_has_permissions 数据都删除了
     * @param $role_id
     * @return int
     */
    public static function delRole($role_id)
    {
        return DB::table('roles')->where('id','=' ,$role_id)->delete();
    }

    /**
     * 条件查询菜单
     * @param $where
     * @param $flag 1:字段有id;0：字段没有id
     * @return array
     */
    public static function getRolesByWhere($where)
    {
        $rt = DB::table('roles')->where('id', '<>', $where['id'])
            ->where(function ($query) use ($where) {
                $query->where('name', '=', $where['name']);
            })->get()->toArray();
        return $rt;
    }

}
